import { createRouter ,createWebHashHistory, createWebHistory} from 'vue-router'
//臭氧对农作物产量影响评估平台
import Observation from '../views/ground/observation/index.vue'
import IronTower from '../views/ground/ironTower/index.vue'
import UAV from '../views/ground/UAV/index.vue'
import UAV2 from '../views/UAV2/index.vue'
import Historydata from '../views/ground/historyData/index.vue'
//
import AOT40 from '../views/ground/AOT40/index.vue'
import JJJcity from '../views/ground/JJJcity/index.vue'
import Hengshui from '../views/ground/hengshui/index.vue'
import JJJsite from '../views/ground/JJJsite/index.vue'
import NationStation from '../views/ground/nationStation/index.vue'
//臭氧综合监测信息处理分析平台
import GrowthCycle from '../views/crops/growthCycle/index.vue'
import OutputTable from '../views/crops/outputTable/index.vue'
import YeardData from '../views/crops/yearData/index.vue'
import Cropanalysyear from '../views/crops/cropAnalysYear/index.vue'
//其他

import Start from '../views/start/index.vue'

import Layout from '../views/Main.vue'
import Login from '../views/login/index.vue'
import User from '../views/user/index.vue' 

//test
import Test from '../components/test.vue'
//import File from '../views/file/index.vue'
import component from 'element-plus/es/components/tree-select/src/tree-select-option.mjs'

const routes = [
    { 
      path: '/',
      component: Layout,
      name: 'main',
      redirect: '/login', // 设置默认页面为login页面
      children: [
        {
          path: 'start',
          meta: { id: 'a', name: '首页', icon: 'HomeFilled', path: '/start', describe: '',  requiresAuth: true },
          component:Start,
        },
        {
          path: 'ground',
          meta: { id: 'b', name: '臭氧综合监测信息处理分析平台', icon:'SemiSelect' , path: '/ground', describe: '', requiresAuth: true },
          children: [
            {
              path: 'observation',
              meta: { id: 'c', name: '臭氧观测', icon: 'MostlyCloudy', path: '/ground/observation', describe: '',  requiresAuth: true },
              component:Observation,
            },
            {
              path: 'ironTower',
              meta: { id: 'd', name: '铁塔数据', icon: 'Coordinate', path: '/ground/ironTower', describe: '',  requiresAuth: true },
              component:IronTower
            }, 
            {
              path: 'UAV',
              meta: { id: 'e', name: '无人机', icon: 'Position', path: '/ground/UAV', describe: '', requiresAuth: true },
              component:UAV
            },
            {
              path: 'UAV2',
              meta: { id: 'e', name: '无人机订正数据页面', icon: 'Position', path: '/ground/UAV2', describe: '', requiresAuth: true },
              component:UAV2
            },
            {
              path:'AOT40',
              meta:{id: 'f', name: 'AOT40臭氧逐日历史数据', icon: 'MostlyCloudy', path: '/ground/AOT40', describe: '', requiresAuth: true },
              component:AOT40
            },
            {
              path:'hengshui',
              meta:{id: 'g', name: '衡水邢台站点污染数据', icon: 'Flag', path: '/ground/hengshui', describe: '', requiresAuth: true },
              component:Hengshui
            },
            {
              path:'JJJcity',
              meta:{id: 'h', name: '京津冀城市空气污染数据', icon: 'OfficeBuilding', path: '/ground/JJJcity', describe: '', requiresAuth: true },
              component:JJJcity
            },
            {
              path:'JJJsite',
              meta:{id: 'i', name: '京津冀站点空气污染数据', icon: 'Location', path: '/ground/JJJsite', describe: '', requiresAuth: true },
              component:JJJsite
            },
            {
              path:'nationStation',
              meta:{id: 'j', name: '全国污染站点数据', icon: 'ChromeFilled', path: '/ground/nationStation', describe: '', requiresAuth: true },
              component:NationStation
            },
            {
              path: 'historyData',
              meta: { id: 'k', name: '气象历史逐日数据', icon:'Collection' , path: '/ground/historyData', describe: '', requiresAuth: true },
              component:Historydata
            },
          ]
        },
        {
          path: 'crops',
          meta: { id: 'l', name: '臭氧对作物产量影响评估平台', icon:'DataLine' , path: '/crops', describe: '', requiresAuth: true },
          children: [
            {
              path:'growthCycle',
              meta:{id: 'm', name: '小麦玉米生长周期', icon: 'Fries', path: '/crops/growthCycle', describe: '', requiresAuth: true },
              component:GrowthCycle
            },
            // {
            //   path:'outputTable',
            //   meta:{id: 'n', name: '作物产量年表', icon: 'Tickets', path: '/crops/outputTable', describe: '', requiresAuth: true },
            //   component:OutputTable
            // },
            {
              path:'yearData',
              meta:{id: 'o', name: '作物年分析数据', icon: 'Tickets', path: '/crops/yearData', describe: '', requiresAuth: true },
              component:YeardData
            },
            {
              path: 'cropAnalysYear',
              meta: { id: 'p', name: '臭氧作物逐年数据', icon:'EditPen' , path: '/crops/cropAnalysYear', describe: '' },
              component: Cropanalysyear
            },
          ]
        },
        {
          path: 'user',
          meta: { id: 'q', name: '用户管理', icon:'user' , path: '/user', describe: '' },
          component:User
        },
      ]
    },

    {
      path: '/test',
      component: Test
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    }
  ]
const router=createRouter({
    //      路由数据
    routes,
    //路由匹配模式
    history:createWebHistory(),

})

//全局前置守卫：检查用户是否登录
// 添加全局路由守卫
router.beforeEach((to, from, next) => {
  // 判断目标路由是否需要认证
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem("token");  // 获取存储的 token
    if (!token) {
      // 如果没有 token，表示未登录，跳转到登录页面
      next({ name: 'Login' });
    } else {
      // 已经登录，继续访问目标页面
      next();
    }
  } else {
    // 如果目标路由不需要认证，直接访问
    next();
  }
})

export default router